<?php

namespace Gairal\CoreBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * Category
 *
 * @ORM\Table(name="gairal_category")
 * @ORM\Entity(repositoryClass="Gairal\CoreBundle\Entity\CategoryRepository")
 * @UniqueEntity(fields="name", message="This category already exists in the database.")
 * @ORM\HasLifecycleCallbacks()
 */
class Category
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=50, unique=true)
     * @Assert\NotBlank()
     * @Assert\Length(min="3")
     */
    private $name;

    /**
     * @var integer
     *
     * @ORM\Column(name="display_order", type="smallint")
     */
    private $displayOrder = 0;
    
    /**
     * @ORM\OneToMany(targetEntity="Gairal\CoreBundle\Entity\Interest", mappedBy="category", orphanRemoval=true)
     * @ORM\JoinColumn(nullable=true)
     */
    private $interests;
    
    /**
     * @ORM\OneToMany(targetEntity="Gairal\CoreBundle\Entity\Skill", mappedBy="category", orphanRemoval=true)
     * @ORM\JoinColumn(nullable=true)
     */
    private $skills;
    
    /**
     * I want tu use a prePersist her
     */
    public function computeDisplayOrder(){
        $maxOrder = $this->getDoctrine()
                ->getManager()
                ->getRepository('GairalCoreBundle:Category')
                ->findMaxOrder();
        $this->setDisplayOrder(++$maxOrder);
    }


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Category
     */
    public function setName($name)
    {
        $this->name = $name;
    
        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set displayOrder
     *
     * @param integer $displayOrder
     * @return Category
     */
    public function setDisplayOrder($displayOrder)
    {
        $this->displayOrder = $displayOrder;
    
        return $this;
    }

    /**
     * Get displayOrder
     *
     * @return integer 
     */
    public function getDisplayOrder()
    {
        return $this->displayOrder;
    }
    
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->interests = new \Doctrine\Common\Collections\ArrayCollection();
        $this->skills = new \Doctrine\Common\Collections\ArrayCollection();
    }
    
    /**
     * Add interests
     *
     * @param \Gairal\CoreBundle\Entity\Interest $interests
     * @return Category
     */
    public function addInterest(\Gairal\CoreBundle\Entity\Interest $interests)
    {
        $this->interests[] = $interests;
    
        return $this;
    }

    /**
     * Remove interests
     *
     * @param \Gairal\CoreBundle\Entity\Interest $interests
     */
    public function removeInterest(\Gairal\CoreBundle\Entity\Interest $interests)
    {
        $this->interests->removeElement($interests);
    }

    /**
     * Get interests
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getInterests()
    {
        return $this->interests;
    }

    /**
     * Add skills
     *
     * @param \Gairal\CoreBundle\Entity\Skill $skills
     * @return Category
     */
    public function addSkill(\Gairal\CoreBundle\Entity\Skill $skills)
    {
        $this->skills[] = $skills;
    
        return $this;
    }

    /**
     * Remove skills
     *
     * @param \Gairal\CoreBundle\Entity\Skill $skills
     */
    public function removeSkill(\Gairal\CoreBundle\Entity\Skill $skills)
    {
        $this->skills->removeElement($skills);
    }

    /**
     * Get skills
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getSkills()
    {
        return $this->skills;
    }
}